home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 April: Mac OS SDK / Dev.CD Apr 96 SDK / Dev.CD Apr 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc Development Framework / ODFDev / ODF / Examples / Hello / Sources / HelloSel.cpp < prev    next >
Encoding:
Text File  |  1995-11-08  |  7.5 KB  |  269 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                HelloSel.cpp
  4. //    Release Version:    $ 1.0d11 $
  5. //
  6. //    Copyright:    © 1995 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #include "Hello.hpp"
  11.  
  12. #ifndef HELLOSEL_H
  13. #include "HelloSel.h"
  14. #endif
  15.  
  16. #ifndef HELLOPRT_H
  17. #include "HelloPrt.h"
  18. #endif
  19.  
  20. #ifndef HELLODEF_H
  21. #include "HelloDef.h"
  22. #endif
  23.  
  24. // ----- Framework Includes -----
  25.  
  26. #ifndef FWPRESEN_H
  27. #include "FWPresen.h"
  28. #endif
  29.  
  30. #ifndef FWUTIL_H
  31. #include "FWUtil.h"
  32. #endif
  33.  
  34. // ----- OS Includes -----
  35.  
  36. #ifndef FWBARRAY_H
  37. #include "FWBArray.h"
  38. #endif
  39.  
  40. // ----- Foundation Layer -----
  41.  
  42. #ifndef FWSUSINK_H
  43. #include "FWSUSink.h"
  44. #endif
  45.  
  46. #ifndef FWSTREAM_H
  47. #include "FWStream.h"
  48. #endif
  49.  
  50. #ifndef FWSTRING_H
  51. #include "FWString.h"
  52. #endif
  53.  
  54. // ----- OpenDoc Includes -----
  55.  
  56. #ifndef SOM_Module_OpenDoc_StdProps_defined
  57. #include <StdProps.xh>
  58. #endif
  59.  
  60. // ----- Macintosh Includes -----
  61.  
  62. #if defined(FW_BUILD_MAC) && !defined(__DRAG__)
  63. #include <Drag.h>
  64. #endif
  65.  
  66. //========================================================================================
  67. //    Runtime information
  68. //========================================================================================
  69.  
  70. #ifdef FW_BUILD_MAC
  71. #pragma segment odfhello
  72. #endif
  73.  
  74. //========================================================================================
  75. //    Constants
  76. //========================================================================================
  77.  
  78. const ODPropertyName kHelloPropText = "Hello:Property:Text";
  79.  
  80. //========================================================================================
  81. //    CLASS CHelloSelection
  82. //========================================================================================
  83.  
  84. //---------------------------------------------------------------------------------------
  85. //    CHelloSelection constructor
  86. //---------------------------------------------------------------------------------------
  87.  
  88. CHelloSelection::CHelloSelection(Environment* ev, CHelloPart* helloPart) :
  89.     FW_CSelection(ev, FALSE, FALSE),
  90.     fHelloPart(helloPart)
  91. {
  92. }
  93.  
  94. //---------------------------------------------------------------------------------------
  95. //    CHelloSelection destructor
  96. //---------------------------------------------------------------------------------------
  97.  
  98. CHelloSelection::~CHelloSelection()
  99. {
  100. }
  101.  
  102. //---------------------------------------------------------------------------------------
  103. //    CHelloSelection::CloseSelection
  104. //---------------------------------------------------------------------------------------
  105.  
  106. void CHelloSelection::CloseSelection(Environment* ev)
  107. {
  108.     // Nothing to do
  109. }
  110.  
  111. //---------------------------------------------------------------------------------------
  112. //    CHelloSelection::ClearSelection
  113. //---------------------------------------------------------------------------------------
  114.  
  115. FW_Boolean CHelloSelection::ClearSelection(Environment* ev)
  116. {
  117.     // Empty the currently selected string
  118.     fHelloPart->ClearTextData();
  119.     fHelloPart->PartChanged(ev);
  120.     return TRUE;
  121. }
  122.  
  123. //---------------------------------------------------------------------------------------
  124. //    CHelloSelection::IsEmpty
  125. //---------------------------------------------------------------------------------------
  126.  
  127. FW_Boolean CHelloSelection::IsEmpty(Environment* ev) const
  128. {
  129.     // Is the current string empty?
  130.     return (fHelloPart->GetTextData().GetLength() == 0);
  131. }
  132.  
  133. //---------------------------------------------------------------------------------------
  134. //    CHelloSelection::SelectAll
  135. //---------------------------------------------------------------------------------------
  136.  
  137. void CHelloSelection::SelectAll(Environment* ev)
  138. {
  139.     // Not applicable
  140. }
  141.  
  142. //----------------------------------------------------------------------------------------
  143. //    CHelloSelection::DoExternalizeSelection
  144. //----------------------------------------------------------------------------------------
  145.  
  146. void CHelloSelection::DoExternalizeSelection(Environment* ev, 
  147.                                              ODStorageUnit* destinationSU, 
  148.                                              FW_CCloneInfo* cloneInfo)
  149. {
  150.     // Get the current selection string
  151.     FW_CString255 string(fHelloPart->GetTextData());
  152.  
  153.     // Write the string to the storage unit, in the part's content format
  154.     FW_CStorageUnitSink suSink(destinationSU, kODPropContents, CHelloPart::kPartKind);
  155.     FW_CWritableStream archive(&suSink);
  156.     unsigned long stringCount = 1;
  157.  
  158.     archive << stringCount;
  159.     FW_CStringArchiver::Write(archive, string);
  160.  
  161. #ifdef FW_BUILD_MAC
  162.     // Also write the string in text format
  163.     FW_SUAddPropValue(ev, destinationSU, kODPropContents, FW_CPart::gMacTEXTDataType);
  164.     FW_CStorageUnitSink sink2(destinationSU, kODPropContents, FW_CPart::gMacTEXTDataType);
  165.     FW_CWritableStream stream(&sink2);
  166.     FW_Char buffer[256];
  167.     string.Export((char*) &buffer);
  168.     stream.Write(&buffer, string.GetByteLength());
  169. #endif
  170. }
  171.  
  172. //----------------------------------------------------------------------------------------
  173. //    CHelloSelection::DoInternalizeSelection
  174. //----------------------------------------------------------------------------------------
  175.  
  176. FW_Boolean CHelloSelection::DoInternalizeSelection(Environment* ev,
  177.                                                    ODStorageUnit* sourceSU, 
  178.                                                    FW_CCloneInfo* cloneInfo)
  179. {
  180.     FW_UNUSED(cloneInfo);
  181.  
  182.     FW_Boolean internalized = FALSE;
  183.     FW_CString255 string("");
  184.  
  185.     if (sourceSU->Exists(ev, kODPropContents, fHelloPart->GetPartKind(ev), 0))
  186.     {
  187.         if (this->Internalize1String(ev, sourceSU, string))
  188.         {
  189.             fHelloPart->SetTextData(string);
  190.             internalized = TRUE;
  191.         }
  192.     }
  193. #ifdef FW_BUILD_MAC
  194.     else if (sourceSU->Exists(ev, kODPropContents, FW_CPart::gMacTEXTDataType, 0))    // 'TEXT'
  195.     {
  196.         if (this->InternalizeText(ev, sourceSU, string))
  197.         {
  198.             fHelloPart->SetTextData(string);
  199.             internalized = TRUE;
  200.         }
  201.     }
  202. #endif
  203.  
  204.     if (internalized)
  205.     {
  206.         // Notify interested parties
  207.         fHelloPart->PartChanged(ev);
  208.     }
  209.  
  210.     return internalized;
  211. }
  212.  
  213. //------------------------------------------------------------------------------
  214. // CHelloSelection::Internalize1String
  215. //------------------------------------------------------------------------------
  216. FW_Boolean CHelloSelection::Internalize1String(Environment* ev, ODStorageUnit* storageUnit,
  217.                                                FW_CString& newString)
  218. {
  219.     short result = FALSE;
  220.  
  221.     storageUnit->Focus(ev, kODPropContents, kODPosUndefined, fHelloPart->GetPartKind(ev), 0, kODPosUndefined);       
  222.     if (storageUnit->GetSize(ev) != 0)
  223.     {
  224.         // Read one string from storage, if possible
  225.         FW_CStorageUnitSink suSink(storageUnit, kODPropContents, fHelloPart->GetPartKind(ev));
  226.         FW_CReadableStream archive(&suSink);
  227.         
  228.         unsigned long stringCount = 0;
  229.         archive >> stringCount;
  230.         if (stringCount > 0)
  231.         {
  232.             FW_CStringArchiver::Read(archive, newString);
  233.             result = TRUE;
  234.         }
  235.     }
  236.  
  237.     return result;
  238. }
  239.  
  240. //----------------------------------------------------------------------------------------
  241. //    CHelloSelection::InternalizeText
  242. //----------------------------------------------------------------------------------------
  243.  
  244. FW_Boolean CHelloSelection::InternalizeText(Environment* ev, 
  245.                                             ODStorageUnit* storageUnit, 
  246.                                             FW_CString& newString)
  247. {
  248.     FW_Boolean result = FALSE;
  249.  
  250. #ifdef FW_BUILD_MAC
  251.     storageUnit->Focus(ev, kODPropContents, kODPosUndefined, FW_CPart::gMacTEXTDataType, 0, kODPosUndefined);
  252.     unsigned long textSize = storageUnit->GetSize(ev);
  253.     if (textSize > 0)
  254.     {
  255.         char buffer[256];
  256.         if (textSize > 255) 
  257.             textSize = 255;
  258.         FW_CByteArray byteArray;
  259.         storageUnit->GetValue(ev, textSize, byteArray);
  260.         byteArray.CopyBuffer(&buffer, textSize);
  261.         newString.Append((const FW_Char*) &buffer, textSize);
  262.         result = TRUE;
  263.     }
  264. #endif
  265.  
  266.     return result;
  267. }
  268.  
  269.